package com.lemon.smartsafety.ui.activity.safe.inspection;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.event.common.PanelEvent;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafetynew.ui.activity.base.BaseFragmentActivity;
import com.lemon.smartsafety.ui.activity.common.PanelEventHandler;
import com.lemon.smartsafety.ui.adapter.safe.data.ItemTag;
import com.lemon.smartsafety.ui.fragment.safe.inspection.PanelFragment;
import com.lemon.smartsafety.utils.app.HUDUtil;

import org.greenrobot.eventbus.Subscribe;

/**
 * 安全巡查主界面 安全巡查【功能界面】
 */
public class SafeInspectionPanelActivity extends BaseFragmentActivity {
    private Fragment mFragment;

    private CompanyInfo mCompanyInfo = null;


    @Override
    protected void callFinish() {
        super.callFinish();
        mFragment = null;
        mCompanyInfo = null;
        AppManager.getAppManager().finishActivity(SafeInspectionPanelActivity.class);
    }

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            mCompanyInfo = (CompanyInfo) getIntent().getSerializableExtra(BundleKey.COMPANY_INFO);
        }
    }

    @Override
    public Fragment getFragment() {
        if (mFragment != null) {
            return mFragment;
        }
        Bundle bundle = new Bundle();
        bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
        mFragment = PanelFragment.newInstance();
        mFragment.setArguments(bundle);
        return mFragment;
    }

    @Override
    protected void initViews() {
        super.initViews();
        if (mCompanyInfo == null) {
            mHUDUtil.showErrorTip(getString(R.string.params_error), new HUDUtil.CallBack() {
                @Override
                public void onCall() {
                    AppManager.getAppManager().finishActivity(SafeInspectionPanelActivity.this);
                }
            });
        }
    }



    @Subscribe
    public void onEventMainThread(PanelEvent<ItemTag> event) {

        if (PanelFragment.class.getName().equals(event.getTag())) {
            PanelEventHandler.panelEventHandle(event, this, mCompanyInfo, mHUDUtil);
        }
    }

    @Override
    protected void initTitle() {
        this.setBarTitle(R.string.safe_title);
    }
}
